package com.asa.projectframe.viewmodel

import android.util.Log
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableInt
import com.asa.common.net.NetModel
import com.asa.common.viewmodel.BaseNetViewModel
import com.asa.projectframe.R
import com.asa.projectframe.intercaces.OnMainTabItemClickListener
import com.asa.projectframe.model.MainModel
import me.tatarka.bindingcollectionadapter2.BR
import me.tatarka.bindingcollectionadapter2.OnItemBind

class MainViewModel:BaseNetViewModel<MainModel>() {
    companion object {
        const val TAB_TYPE_HOME = 0
        const val TAB_TYPE_CATEGORY = 1
        const val TAB_TYPE_SQUARE = 2
        const val TAB_TYPE_MINE = 3
    }

    private val titleTab = arrayListOf("首页","分类","广场","我的")

    val pagePos = ObservableInt(0)
    val tabItems = ObservableArrayList<String>()
    val tabItemBinding = OnItemBind<String> { itemBinding, position, item ->
        itemBinding.set(BR.tabName, R.layout.item_home_tab)
            .bindExtra(BR.selectedPosition, pagePos)
            .bindExtra(BR.clickListener, onSquareTabItemClick)
            .bindExtra(BR.position, position)
    }

    val pageItems = ObservableArrayList<BaseNetViewModel<out NetModel>>()
    val pageItemBinding = OnItemBind<BaseNetViewModel<NetModel>>{itemBinding, position, item ->
        when(position){
            0->itemBinding.set(BR.viewModel,R.layout.fragmment_home)
            1->itemBinding.set(BR.viewModel,R.layout.fragment_category)
            2->itemBinding.set(BR.viewModel,R.layout.fragment_square)
            3->itemBinding.set(BR.viewModel,R.layout.fragment_mine)
        }

    }

    override fun onCreate() {
        super.onCreate()
        val items = ObservableArrayList<String>()
        val pageItems = ObservableArrayList<BaseNetViewModel<out NetModel>>()
        titleTab.forEachIndexed {position,item->
            items.add(item)
            when(position){
                0->pageItems.add(HomeViewModel())
                1->pageItems.add(CategoryViewModel())
                2->pageItems.add(SquareViewModel())
                3->pageItems.add(MineViewModel())
            }
        }
        tabItems.clear()
        tabItems.addAll(items)
        this.pageItems.clear()
        this.pageItems.addAll(pageItems)
    }

    override fun createNetModel(): MainModel {
        return MainModel()
    }

    // tab点击
    private val onSquareTabItemClick = object : OnMainTabItemClickListener {
        override fun click(position: Int) {
            pagePos.set(position)
            when (position) {
                else -> {
                    pagePos.set(position)
                }
            }
        }
    }

}